﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Media.Imaging;
using System.IO;
using System.Drawing.Imaging;

namespace bind_n_find
{
    class imaging_c
    {
        /// <summary>
        /// Convert System.Drawing.Image to System.Windows.Media.Imaging.BitmapImage
        /// </summary>
        /// <param name="image"></param>
        /// <returns></returns>
        public static BitmapImage image_to_bmp(Image image)
        {
            BitmapImage bi = new BitmapImage();
            bi.BeginInit();
         //   MemoryStream ms = new MemoryStream();
            if (bi.StreamSource == null)
            {
                bi.StreamSource = new MemoryStream();
            }
            try
            {
                image.Save(bi.StreamSource, ImageFormat.Png);
            }
            catch (System.ArgumentNullException ex1)
            {
                string s = ex1.Message;
            }
            catch (System.Runtime.InteropServices.ExternalException ex2)
            {
                string s = ex2.Message;
            }
            catch (Exception ex3)
            {
                string s = ex3.Message;
            }

         //   ms.Seek(0, SeekOrigin.Begin);
          //  bi.StreamSource = ms;
            bi.EndInit();
            return bi;
        }

        /// <summary>
        /// Convert System.Windows.Media.Imaging.BitmapImage to System.Drawing.Image
        /// </summary>
        /// <param name="bi"></param>
        /// <returns></returns>
        public static Image bmp_to_image(BitmapImage bi)
        {
            try
            {
             /*   MemoryStream TransportStream = new MemoryStream();
                BitmapEncoder enc = new BmpBitmapEncoder();
                enc.Frames.Add(BitmapFrame.Create(bi.StreamSource));
                enc.Save(TransportStream);
                return new System.Drawing.Bitmap(TransportStream);*/
                return new Bitmap(bi.StreamSource);
            }
            catch {  return null; }
            return null;
        }


    }
}
